<template>
    <div class="lrxtgg">
      <quill-editor
            class="ql-editor"
            ref="myQuillEditor"
            v-model="context"
            :options="editorOption"
            @blur="onEditorBlur($event)"
            @focus="onEditorFocus($event)"
            @change="onEditorChange($event)"
          >
          </quill-editor>
    </div>
</template>

<script>
// 引入富文本编辑器
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

import { quillEditor } from 'vue-quill-editor'
// 代码高亮
import hljs from 'highlight.js'
import 'highlight.js/styles/xcode.css'
// import 'highlight.js/styles/monokai-sublime.css'

export default {
  name: 'Lrxtgg',
  components: {
    quillEditor
  },
  props: {
    value: String
  },
  data () {
    return {
      // value: '',
      description: '',
      editorOption: {
        // 编辑器配置
        placeholder: '',
        modules: {
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'], // 加粗，斜体，下划线，删除线
            // [{ header: 1 }, { header: 2 }], // 标题，键值对的形式；1、2表示字体大小
            [{ list: 'ordered' }, { list: 'bullet' }], // 列表
            ['blockquote', 'code-block'], // 引用，代码块
            // [{ script: 'sub' }, { script: 'super' }], // 上下标
            // [{ indent: '-1' }, { indent: '+1' }], // 缩进
            // [{ direction: 'rtl' }], // 文本方向
            // [{ size: ['small', false, 'large', 'huge'] }], // 字体大小
            // [{ header: [1, 2, 3, 4, 5, 6, false] }], // 几级标题
            // [{ color: [] }, { background: [] }], // 字体颜色，字体背景颜色
            // [{ font: [] }], // 字体
            // [{ align: [] }], // 对齐方式
            // ['clean'], // 清除字体样式
            ['link', 'image'] // 上传图片、上传视频 ['image', 'video']
          ],
          syntax: {
            highlight: text => {
              return hljs.highlightAuto(text).value // 这里就是代码高亮需要配置的地方
            }
          }

        }
      }
    }
  },
  computed: {
    context: {
      get () {
        return this.value
      },
      set (val) {
        // this.value = val
      }
    }
  },
  created () {},
  mounted () {
    // const range = quillEditor.getSelection()
    // if (range) {
    //   if (range.length === 0) {
    //     console.log('User cursor is at index', range.index)
    //   } else {
    //     var text = quillEditor.getText(range.index, range.length)
    //     console.log('User has highlighted: ', text)
    //   }
    // } else {
    //   console.log('User cursor is not in editor')
    // }
  },
  methods: {
    /** =====================富文本编辑器====================== */
    // 准备编辑器
    onEditorReady (editor) {},
    onEditorBlur () {
      // const quill = this.$refs.myQuillEditor.quill
      // console.log(quill)
      // quill.setSelection()
      // this.$refs.myQuillEditor.quill.setSelection(this.value.length)
    }, // 失去焦点事件context
    onEditorFocus () {

    }, // 获得焦点事件
    // 内容改变事件
    onEditorChange (val) {
      // this.value = val.html
      this.$emit('input', val.html)
      // console.log(val)
    },
    // 转码
    escapeStringHTML (content) {
      content = content.replace(/&lt;/g, '<')
      content = content.replace(/&gt;/g, '>')
      return content
    }
  },
  destroyed () {},
  activated () {},
  deactivated () {}
}
</script>

<style lang="less" scoped>
.lrxtgg {
  .quill-editor /deep/ .ql-container {
    min-height: 460px;
  }
}
/deep/ .hljs-attribute{
  color: #1495fa!important;
}
.ql-snow .ql-editor pre.ql-syntax {
  color: #1495fa!important;
}
</style>
